package com.piesat.quake.business.system.api;


import com.piesat.common.vo.ResultVO;
import com.piesat.quake.business.system.model.SysUser;
import com.piesat.quake.business.system.model.dto.UserDTO;
import com.piesat.quake.business.system.service.SysUserService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-04-22
 */
@Api(tags = "系统：用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user")
public class SysUserController {

    private final PasswordEncoder passwordEncoder;
    private final SysUserService userService;
    private final String DEFAULT_PWD = "123456";

    @ApiOperation(value = "查询用户", notes = "分页查询用户")
    @GetMapping
    @PreAuthorize("@el.check('admin')")
    public ResultVO query(UserDTO dto) {
        return ResultVO.success(userService.selectPage(dto));
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @PostMapping
    @PreAuthorize("@el.check('admin')")
    public ResultVO create(@RequestBody SysUser user) {
        user.setPassword(passwordEncoder.encode(DEFAULT_PWD));
        user.setAuditState(1);
        user.setEnabled(true);
        userService.create(user);
        return ResultVO.success("创建用户成功");
    }

    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PutMapping
    public ResultVO update(@RequestBody SysUser user) {
        userService.update(user);
        return ResultVO.success("更新用户成功");
    }


    @ApiOperation(value = "删除用户", notes = "删除用户")
    @DeleteMapping
    @PreAuthorize("@el.check('admin')")
    public ResultVO delete(String id) {
        userService.delete(id);
        return ResultVO.success("删除用户成功");
    }

    @ApiOperation(value = "修改密码", notes = "修改用户密码")
    @PutMapping("updatePassword")
    public ResultVO updatePassword(String oldPassword,String newPassword) throws Exception {
        userService.updatePassword(oldPassword,newPassword);
        return ResultVO.success("密码修改成功");
    }

    @ApiOperation(value = "重置密码", notes = "重置用户密码")
    @PutMapping("resetPassword")
    @PreAuthorize("@el.check('admin')")
    public ResultVO resetPassword(String userId) throws Exception {
        userService.resetPassword(userId);
        return ResultVO.success("密码重置成功");
    }

    @ApiOperation(value = "上传帮助文档")
    @PostMapping("uploadDoc")
    @PreAuthorize("@el.check('admin')")
    public ResultVO resetPassword(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        boolean b = userService.uploadDoc(multipartFile);
        if (b) {
            return ResultVO.success("帮助文档上传成功");
        }
        return ResultVO.failure("帮助文档上传失败");
    }

}

